package com.ssy.lingxi.after.sale.utils;

import com.ssy.lingxi.common.constant.order.OrderTypeEnum;
import com.ssy.lingxi.workflow.api.model.contant.ProcessEnum;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;
import java.util.stream.Stream;

/**
 * @author ds
 * @version 2.0.0
 * @date 2021/8/12
 */
public class ProcessUtil {
    //退货工作流code
    private static final List<Integer> RETURN_GOODS_CODE_LIST = new ArrayList<>();
    //退货工作流
    private static final ProcessEnum[] RETURN_GOODS_PROCESS ={
            ProcessEnum.AFTER_SALE_RETURN_GOODS_OUTER_VERIFY,
            ProcessEnum.AFTER_SALE_RETURN_GOODS_MANUAL_DELIVERY,
            ProcessEnum.AFTER_SALE_RETURN_GOODS_VERIFY_OFFLINE_LOGISTICS,
            ProcessEnum.AFTER_SALE_RETURN_GOODS_CONTRACT_VERIFY,
            ProcessEnum.AFTER_SALE_RETURN_GOODS_CONTRACT_MANUAL_DELIVERY,
            ProcessEnum.AFTER_SALE_RETURN_GOODS_VERIFY_CONTRACT_OFFLINE_LOGISTICS,
            ProcessEnum.AFTER_SALE_RETURN_GOODS_PF_MANUAL_DELIVERY
    };

    //换货工作流code
    private static final List<Integer> REPLACE_GOODS_CODE_LIST = new ArrayList<>();
    //换货工作流
    private static final ProcessEnum[] REPLACE_GOODS_PROCESS ={
            ProcessEnum.AFTER_SALE_REPLACE_GOODS_OUTER_VERIFY,
            ProcessEnum.AFTER_SALE_REPLACE_GOODS_MANUAL_DELIVERY,
            ProcessEnum.AFTER_SALE_REPLACE_GOODS_OUTER_VERIFY_OFFLINE_LOGISTICS
    };

    static {
        for (ProcessEnum o : RETURN_GOODS_PROCESS) {
            RETURN_GOODS_CODE_LIST.add(o.getCode());
        }
        for (ProcessEnum o : REPLACE_GOODS_PROCESS) {
            REPLACE_GOODS_CODE_LIST.add(o.getCode());
        }
    }

    private static List<Integer> getCodeByEnum(ProcessEnum... processEnums){
        return Arrays.stream(processEnums).map(ProcessEnum::getCode).collect(Collectors.toList());
    }

    /**
     * 退货：是否为退货流程
     * @param taskType
     * @return
     */
    public static boolean isReturnGoodsProcess(Integer taskType){
        return RETURN_GOODS_CODE_LIST.contains(taskType);
    }

    /**
     * 退货：判断是否手工单[无收发货单的流程]
     * @param taskType
     * @return
     */
    public static boolean isMannualReturnOfReturnGoods(Integer taskType){
        List<Integer> codeByEnum = getCodeByEnum(ProcessEnum.AFTER_SALE_RETURN_GOODS_MANUAL_DELIVERY,
                                                ProcessEnum.AFTER_SALE_RETURN_GOODS_CONTRACT_MANUAL_DELIVERY,
                                                ProcessEnum.AFTER_SALE_RETURN_GOODS_PF_MANUAL_DELIVERY);
        return codeByEnum.contains(taskType);
    }

    /**
     * 退货：判断是否线下物流
     * @param taskType
     * @return
     */
    public static boolean isOfflineLogisticsOfReturnGoods(Integer taskType){
        List<Integer> codeByEnum = getCodeByEnum(ProcessEnum.AFTER_SALE_RETURN_GOODS_VERIFY_OFFLINE_LOGISTICS,
                                                ProcessEnum.AFTER_SALE_RETURN_GOODS_VERIFY_CONTRACT_OFFLINE_LOGISTICS);
        return codeByEnum.contains(taskType);
    }

    /**
     * 退货：判断是否合同流程
     * @param taskType
     * @return
     */
    public static boolean isContractOfReturnGoods(Integer taskType){
        List<Integer> codeByEnum = getCodeByEnum(ProcessEnum.AFTER_SALE_RETURN_GOODS_CONTRACT_VERIFY,
                                                ProcessEnum.AFTER_SALE_RETURN_GOODS_CONTRACT_MANUAL_DELIVERY,
                                                ProcessEnum.AFTER_SALE_RETURN_GOODS_VERIFY_CONTRACT_OFFLINE_LOGISTICS);
        return codeByEnum.contains(taskType);
    }

    /**
     * 退货：判断是否合同流程
     * @param orderType
     * @return
     */
    public static boolean isSrmContractOfReturnGoods(Integer orderType){
        List<Integer> codeByEnum = Stream.of(OrderTypeEnum.QUERY_PRICE_CONTRACT.getCode(),
                OrderTypeEnum.PRICE_COMPETITION_CONTRACT_PURCHASE.getCode(),
                OrderTypeEnum.PURCHASE_CONTRACT_BIDDING.getCode(),
                OrderTypeEnum.REQUISITION_TO_PURCHASE.getCode())
                .collect(Collectors.toList());
        return codeByEnum.contains(orderType);
    }

    /**
     * 换货：是否为换货流程
     * @param taskType
     * @return
     */
    public static boolean isReplaceGoodsProcess(Integer taskType){
        return REPLACE_GOODS_CODE_LIST.contains(taskType);
    }

    /**
     * 换货：判断是否线下物流
     * @param taskType
     * @return
     */
    public static boolean isOfflineLogisticsOfReplaceGoods(Integer taskType){
        List<Integer> codeByEnum = getCodeByEnum(ProcessEnum.AFTER_SALE_REPLACE_GOODS_OUTER_VERIFY_OFFLINE_LOGISTICS);
        return codeByEnum.contains(taskType);
    }

    /**
     * 换货：判断是否手工单[无收发货单]
     * @param taskType
     * @return
     */
    public static boolean isMannualOfReplaceGoods(Integer taskType){
        List<Integer> codeByEnum = getCodeByEnum(ProcessEnum.AFTER_SALE_REPLACE_GOODS_MANUAL_DELIVERY);
        return codeByEnum.contains(taskType);
    }
}
